/*
* task_scheduler.c
*
*  Created on: Jul 26, 2024
*      Author: vuxtvg@163.com
*/

#include "task_scheduler.h"

void TaskHandler(void)
{
        for (uint8_t i = 0; i < TASK_NUM_MAX; i++)
        {
                if (g_taskComps[i].run && g_taskComps[i].enabled)
                {
                        g_taskComps[i].run = 0;
                        g_taskComps[i].runCount++;
                        g_taskComps[i].pTaskFuncCb();
                }
        }
}

void TaskScheduleCb(void)
{
        for (uint8_t i = 0; i < TASK_NUM_MAX; i++)
        {
                if (g_taskComps[i].timCount && g_taskComps[i].enabled)
                {
                        g_taskComps[i].timCount--;
                        if (g_taskComps[i].timCount == 0)
                        {
                                g_taskComps[i].run = 1;
                                g_taskComps[i].timCount = g_taskComps[i].timRload;
                        }
                }
        }
}

void SetTaskEnabled(void (*taskFunc)(void), uint8_t state)
{
        for (uint8_t i = 0; i < TASK_NUM_MAX; i++)
        {
            if (g_taskComps[i].pTaskFuncCb == taskFunc)
            {
                g_taskComps[i].enabled = state;
            }
        }
}

uint32_t GetTaskRunCount(void (*taskFunc)(void))
{
     for (uint8_t i = 0; i < TASK_NUM_MAX; i++)
        {
            if (g_taskComps[i].pTaskFuncCb == taskFunc)
            {
                return g_taskComps[i].runCount;
            }
        }
        return 0;  // 如果没有找到匹配的任务函数，返回0
}

void ExecuteOnMultiple(void (*taskFunc)(void), uint32_t multiple,
                void (*ActionFunc)(void))
{
        uint32_t runCount = GetTaskRunCount(taskFunc);
        if (runCount % multiple == 0)
        {
                ActionFunc();
        }
}

void TaskInit(void)
{
//        TaskScheduleCbReg(TaskScheduleCb);
//        InitHashTable();
}
